package com.indusfo.spc.service.impl;


import com.indusfo.spc.exception.GlobalException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.ProcessBlxxMapper;
import com.indusfo.spc.pojo.ProcessBlxx;
import com.indusfo.spc.service.ProcessBlxxService;
import com.indusfo.spc.vo.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author louk
 * @date 2019/9/2 10:38
 */
@Service
public class ProcessBlxxServiceImpl implements ProcessBlxxService {
    // 声明日志的静态变量
    private static final Logger logger = LoggerFactory.getLogger(ProcessBlxxServiceImpl.class);
    @Resource
    private ProcessBlxxMapper processBlxxMapper;

    /**
     * 分页查询 pagesize pageindex
     * 模糊查询 dim
     * ID查询  Pid
     * 返回总数 count
     * @param processBlxx
     * @return
     */
    @Override
    public JSONObject queryProcessBlxx(ProcessBlxx processBlxx) {
        try {
            //获取页面的数据个数
            Integer pagesize = processBlxx.getPagesize();
            //获取查询的页数
            Integer pageindex = processBlxx.getPageindex();
            if(pagesize!=null && pageindex !=null){
                processBlxx.setIncept(pagesize*(pageindex-1));
            }
            List<ProcessBlxx> listProcessBlxx=processBlxxMapper.queryProcessBlxx(processBlxx);
            if(listProcessBlxx.isEmpty()){
                return  JSONObject.oK("没有查询到相关消息",listProcessBlxx,0);
            }
            //如果页数和页面数量都不为空的情况下进行计数

            Integer count= processBlxxMapper.countProcessBlxx(processBlxx);
            return JSONObject.oK("查询成功",listProcessBlxx,count);
        } catch (GlobalException e) {
            // 捕获异常,打印并返回数
            logger.error(e.getMessage(), e);
            return JSONObject.build(500, e.getMessage());

        }

    }

    /*
    非空判断 长度验证   checkParam
    查重判断 checkParamsNotRepeat
    默认值赋值 setDataState(1)
    */
    @Override
    public JSONObject insertProcessBlxx(ProcessBlxx processBlxx) {
        /**
         * 这里还要判断哪些不为空
         */
        try{
            if(processBlxx.getProcessBlxxId()!=null){
                throw new ModifyFailedException("新增不良现象编号不能存在");
            }
            if(processBlxx.getDataState()==null){
                processBlxx.setDataState(1);
            }
            //非空判断 长度验证   checkParam
            checkParam(processBlxx);
            //查重判断 checkParamsNotRepeat
            //            checkParamsNotRepeat(processBlxx);
            //            //默认值赋值
            processBlxx.setDataState(1);
            Integer row=processBlxxMapper.insertProcessBlxx(processBlxx);
            if(row==0){
                throw new ModifyFailedException("新增不良现象失败");
            }
            return JSONObject.oK("新增成功");
        }catch(GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 多数据删除 Long[] bldcIds
     * 级联删除 delteBldcByBldcTypeId
     * @param ProcessProcessBlxxIds
     * @param IDataState
     * @return
     */
    @Override
    public JSONObject deleteProcessBlxx(List<Integer> ProcessProcessBlxxIds, Integer IDataState) {
        try{
            //判断传入的参数是否为null
            if(IDataState == null) {
                //为null抛出异常
                throw new ParamsErrorException("数据状态不能为空");
            }else if(IDataState != 1 && IDataState != 2 && IDataState != 3) {
                throw new ParamsErrorException("数据状态错误");
            }
            if(ProcessProcessBlxxIds == null) {
                throw new ParamsErrorException("请选择设备资料");
            }
            //调用删除的接口
            int row = processBlxxMapper.deleteProcessBlxx(ProcessProcessBlxxIds,IDataState);
            //声明msg字符串，用来存抛出的异常
            String msg = "";
            if (row == 0) {
                // 判断传入的数据状态参数lDataState,返回相应信息
                switch (IDataState) {
                    case 1:
                        throw new ModifyFailedException("启用失败");
                    case 2:
                        throw new ModifyFailedException("删除失败");
                    case 3:
                        throw new ModifyFailedException("停用失败");
                    default:
                        break;
                }
            } else {
                switch (IDataState) {
                    case 1:
                        msg = "启用成功";break;
                    case 2:
                        msg = "删除成功";break;
                    case 3:
                        msg = "停用成功";break;
                    default:
                        break;
                }
            }
            return JSONObject.oK(msg);
        }catch(GlobalException e) {
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }

    }

    /**
     * 非空判断 checkParam
     * 查重判断 checkParamsNotRepeat    疑问不需要判断了吧
     * 默认值赋值 setDataState(1)
     * @param processBlxx
     * @return
     */
    @Override
    public JSONObject updateProcessBlxx(ProcessBlxx processBlxx) {
        try {
            //判断传入的参数是否为null
            if(processBlxx.getBlxxId()==null) {
                //为null则抛出异常
                throw new ParamsErrorException("修改不良现象id不能为空");
            }
            if(processBlxx.getDataState()==null){
                processBlxx.setDataState(1);
            }
            //查重判断 checkParamsNotRepeat
            checkParam(processBlxx);

            //调用修改的接口
            int row = processBlxxMapper.updateProcessBlxx(processBlxx);
            if(row == 0) {
                throw new ModifyFailedException("数据修改失败");
            }
            return JSONObject.oK("数据修改成功");
        }catch (GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    @Override
    public JSONObject batchInsertProcessBlxx(List<Integer> blxxIds, Integer lProFlow) {
        try{
            if(blxxIds==null){
                throw new ModifyFailedException("不良现象编号不存在");
            }
            if(lProFlow==null){
                throw new ModifyFailedException("工序流程编号不存在");
            }
            Integer row=0;
            for(int i=0;i<blxxIds.size();i++){
                ProcessBlxx processBlxx=new ProcessBlxx();
                processBlxx.setBlxxId(blxxIds.get(i));
                processBlxx.setlProFlow(lProFlow);
                processBlxx.setDataState(1);
                Integer Counts = processBlxxMapper.checkProcessBlxx(processBlxx);
                if (Counts !=0) {
                    throw new ParamsErrorException("该不良现象已存在");
                }else{
                     row=row+processBlxxMapper.insertProcessBlxx(processBlxx);
                }


            }
            if(row==0){
                throw new ModifyFailedException("新增不良现象失败");
            }
            return JSONObject.oK("新增成功",row);
        }catch(GlobalException e) {//捕获自定义异常
            logger.error(e.getMessage());
            return JSONObject.build(500, e.getMessage());
        }
    }

    public void checkParam(ProcessBlxx processBlxx){
        if (processBlxx.getBlxxId() == null) {
            throw new ParamsErrorException("不良现象类型不能为空");
        }
        if (processBlxx.getlProFlow() == null) {
            throw new ParamsErrorException("不良现象类型不能为空");
        }

        if(processBlxx.getRemark()!=null){
            if (processBlxx.getRemark().getBytes().length> 100) {
                throw new ParamsErrorException("说明字数超过规定长度");
            }
        }

        // 名称不能重复
        Integer Counts = processBlxxMapper.checkProcessBlxx(processBlxx);
        if (Counts !=0) {
            throw new ParamsErrorException("该不良现象已存在");
        }
    }

}
